<template>
  <div class="app-container">
    <div
      style="
        display: flex;
        justify-content: end;
        align-items: center;
        margin: 0px 0px 10px 0px;
      "
    >
      <el-input
        v-model="queryParams.userName"
        style="max-width: 200px; margin-left: 10px"
        placeholder="请输入用户姓名"
        @change="handleQuery"
      >
        <template #append>
          <el-button :icon="Search" />
        </template>
      </el-input>
    </div>
    <el-table
      v-loading="loading"
      :data="postList"
      :default-sort="{ prop: 'postId', order: 'descending' }"
    >
      <el-table-column align="center" prop="userName" label="姓名" />
      <el-table-column align="center" prop="signInTime" label="签到时间" />
      <el-table-column align="center" prop="signInDay" label="签到日期" />
      <el-table-column align="center" prop="signInAddress" label="签到地址" />
      <el-table-column
        align="center"
        prop="signInAddressDetail"
        label="经纬度"
      />
      <el-table-column align="center" prop="visitObject" label="拜访对象" />
      <el-table-column align="center" label="是否迟到">
        <template #default="scope">
          <span v-if="scope.row.isLate == false">否</span>
          <span v-if="scope.row.isLate == true">是</span>
          <span
            v-if="scope.row.isLate != false && scope.row.isLate != true"
          ></span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="remark" label="签到备注" />
      <el-table-column label="操作" align="center" width="180">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['signIn:time:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['signIn:time:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改签到记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules">
        <el-form-item label="是否迟到" prop="isLate">
          <el-radio-group v-model="form.isLate">
            <el-radio :value="false">没有迟到</el-radio>
            <el-radio :value="true">迟到</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="签到备注" prop="remark">
          <el-input v-model="form.remark" placeholder="签到备注" />
        </el-form-item>

        <el-form-item label="签到地址" prop="signInAddress">
          <el-input v-model="form.signInAddress" placeholder="请输入地址" />
        </el-form-item>

        <el-form-item label="拜访对象" prop="clientId">
          <el-select v-model="form.clientId" placeholder="请选择拜访对象">
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in clientList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { Search } from "@element-plus/icons-vue";
import { ref } from "vue";
import {
  getSignRecordApi,
  deleteSignRecordApi,
  getSignRecordByIdApi,
  putSignRecordApi,
} from "@/api/sign/time";
import { getClientApi } from "@/api/customerManagement/index";

const { proxy } = getCurrentInstance();
const postList = ref([]);
const clientList = ref([]);
const loading = ref(true);
const total = ref(0);
const open = ref(false);
const title = ref("");
const data = reactive({
  form: {
    isLate: false,
    remark: "",
    signInAddress: "",
    clientId: "",
  },

  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: "",
  },

  rules: {
    // title: [{ required: true, message: "推荐标题不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询签到记录列表 */
function getList() {
  loading.value = true;
  getSignRecordApi(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

/**
 * 按名字搜索签到记录
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/**
 * 获取拜访对象列表
 */
const getClientList = () => {
  clientList.value = [];
  getClientApi({
    pageNum: 1,
    pageSize: 999,
  }).then((response) => {
    response.rows.map((item) => {
      clientList.value.push({
        name: item.name,
        id: item.id,
      });
    });
  });
};
getClientList();

/**
 * 重置
 */
const rest = () => {
  form.value = {
    isLate: false,
    remark: "",
    signInAddress: "",
    clientId: "",
  };
  proxy.resetForm("postRef");
};

/** 修改按钮操作 */
function handleUpdate(row) {
  rest();
  const postId = row.id;
  getSignRecordByIdApi(postId).then((response) => {
    form.value = {
      id: response.data.id,
      isLate: response.data.isLate,
      remark: response.data.remark,
      signInAddress: response.data.signInAddress,
      clientId: response.data.clientId,
    };
    open.value = true;
    title.value = "修改广告配置";
  });
}

/** 取消按钮 */
function cancel() {
  rest();
  open.value = false;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        putSignRecordApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.id;
  proxy.$modal
    .confirm(
      `是否确认删除用户${row.userName}时间为${row.signInTime}的签到记录数据项？`
    )
    .then(function () {
      return deleteSignRecordApi(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
<style scoped>
:deep(.el-form-item) {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
</style>
